package com.bci.pwtz.controller.app;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.remote.ListResponse;
import com.bci.pwtz.common.remote.ObjResponse;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.KzsxsbzpListReq;
import com.bci.pwtz.common.remote.req.ZykzsxsbzpReq;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.ConfigMapper;
import com.bci.pwtz.mysql.model.Config;
import com.bci.pwtz.mysql.model.Zykzsxsbzp;
import com.bci.pwtz.service.PictureUploadAndDownloadService;
import com.bci.pwtz.service.ZykzsxsbzpService;
@Controller
@RequestMapping("/api/zykzsxsbzp")
public class ZykzsxsbzpAppController extends AbstractController
{
@Autowired
ZykzsxsbzpService service;
@Autowired
PictureUploadAndDownloadService picture;
@Autowired
ConfigMapper configMapper;
/**
 * 反孤岛装置的list查询
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:25:21<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public ListResponse<Zykzsxsbzp> list(HttpServletRequest request)
{
    KzsxsbzpListReq req = null;
    ListResponse<Zykzsxsbzp> resp = new ListResponse<Zykzsxsbzp>();
    PaginationList<Zykzsxsbzp> list = new PaginationList<Zykzsxsbzp>();
    try
    {
        req =getJsonToObj(request, KzsxsbzpListReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空");
            return resp;
        }
        if( req.getTimestamp() == null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("时间戳为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;  
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(req.getCompanyId()==null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp; 
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        list=service.findZykzsxsbzp(req.getCompanyId(),PwtzConstants.PORTAL_TYPE_APP,req.getMedLineCodes(), req.getBeginTime(), null, req.getPageSize(),req.getPage(),null);
        if (list != null)
        {
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("查询扩展属性设备照片列表信息成功");
            resp.setTimestamp(req.getTimestamp());
            resp.setObjs(list);
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("查询扩展属性设备照片列表信息调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}

/**
 * 新增反孤岛装置信息
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:25:38<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public ObjResponse<Zykzsxsbzp> add(HttpServletRequest request)
{
    ObjResponse<Zykzsxsbzp> resp = new ObjResponse<Zykzsxsbzp>();
    ZykzsxsbzpReq req = null;
    try
    {
        req = (ZykzsxsbzpReq) getJsonToObj(request, ZykzsxsbzpReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空");
            return resp;
        }
        if(req.getTimestamp() == null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("时间戳为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        Zykzsxsbzp obj =req.getObj();
        int result=service.selectMaxIndexNumber(obj.getDeviceid(), obj.getPhototype());
        obj.setIndexnumber(result+1);
        obj=service.add(obj);
        if (obj != null)
        {
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("新增扩展设备属性照片成功");
            resp.setTimestamp(req.getTimestamp());
            if(req.getObj().getImagestring()!=null&&req.getObj().getImagestring()!=""){
                Config config=configMapper.selectByKey("DevicePICPath").get(0);
                String configpath=config.getConfigValue().replaceAll("@COMPANYID", req.getObj().getCompanyid().toString());
                String fileurl=request.getSession().getServletContext().getRealPath("/")+configpath;
//                picture.upload(req.getObj().getImagestring(), fileurl, req.getObj().getPhotoname());
//                picture.upload(req.getObj().getSmallimagestring(), fileurl, req.getObj().getPhotoname().replaceAll(".", "small."));
            }
            setLog(obj.getDeviceid(), obj.getCompanyid(), 0, AbstractController.add, "新增扩展属性设备照片["+obj.getPhotoname()+"]成功", req.getUserId());
            resp.setObj(obj);
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("新增扩展设备属性照片调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}

/**
 * 修改反孤岛装置信息
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:25:55<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
public ObjResponse<Zykzsxsbzp> update(HttpServletRequest request)
{
    ObjResponse<Zykzsxsbzp> resp = new ObjResponse<Zykzsxsbzp>();
    ZykzsxsbzpReq req = null;
    int result = 0;
    try
    {
        req = (ZykzsxsbzpReq) getJsonToObj(request, ZykzsxsbzpReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空");
            return resp;
        }
        if( req.getTimestamp() == null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("时间戳为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(req.getObj()==null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        result = service.update(req.getObj());
        if (result > 0)
        {
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("修改扩展属性设备照片成功");
            if(req.getObj().getImagestring()!=null&&req.getObj().getImagestring()!=""){
                Config config=configMapper.selectByKey("DevicePICPath").get(0);
                String configpath=config.getConfigValue().replaceAll("@COMPANYID", req.getObj().getCompanyid().toString());
                String fileurl=request.getSession().getServletContext().getRealPath("/")+configpath;
//                picture.upload(req.getObj().getImagestring(), fileurl, req.getObj().getPhotoname());
//                picture.upload(req.getObj().getSmallimagestring(), fileurl, req.getObj().getPhotoname().replaceAll(".", "small."));
            }
            resp.setTimestamp(req.getTimestamp());
            resp.setObj(req.getObj());
            setLog(req.getObj().getDeviceid(), req.getObj().getCompanyid(), 0, AbstractController.update, "修改扩展属性设备照片["+req.getObj().getPhotoname()+"]成功", req.getUserId());
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("修改扩展属性设备照片调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}

/**
 * 删除反孤岛装置信息
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:26:15<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public ObjResponse<Zykzsxsbzp> delete(HttpServletRequest request)
{
    ObjResponse<Zykzsxsbzp> resp = new ObjResponse<Zykzsxsbzp>();
    ZykzsxsbzpReq req = null;
    int result = 0;
    try
    {
        req = (ZykzsxsbzpReq) getJsonToObj(request, ZykzsxsbzpReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空");
            return resp;
        }
        if( req.getTimestamp() == null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("时间戳为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(req.getObj()==null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        Zykzsxsbzp kzsxsbzp=service.load(req.getObj().getId());
        result = service.delete(req.getObj().getId());
        if (result > 0)
        {
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("删除扩展属性设备照片成功");
            resp.setTimestamp(req.getTimestamp());
            resp.setObj(req.getObj());
            setLog(req.getObj().getId(), req.getObj().getCompanyid(), 0, AbstractController.delete, "删除扩展属性设备照片["+kzsxsbzp.getPhotoname()+"]成功", req.getUserId());
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("删除扩展属性设备照片调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (CanNotDeleteException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("存在依耐关系，不能删除");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}
}
